package com.cv4j.app.drawer;

import ohos.agp.components.ListContainer;
import ohos.app.Context;

import com.cv4j.app.ResourceTable;
import com.lxj.xpopup.core.DrawerPopupView;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: Drawer弹窗
 *
 * @since 2021-05-20
 */
public class DrawerPopup extends DrawerPopupView {
    MenuManager.MenuType curType;

    private ItemClickedListener itemClickedListener;

    /**
     * DrawerPopup
     *
     * @param context context
     * @param curType curType
     */
    public DrawerPopup(Context context, MenuManager.MenuType curType) {
        super(context, null);
        this.curType = curType;
    }

    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_layout_left_menu;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        List<MenuItem> list = new ArrayList<>();
        list.add(new MenuItem(MenuManager.MenuType.HOME, ResourceTable.Media_menu_safinfo, curType));
        list.add(new MenuItem(MenuManager.MenuType.IO, ResourceTable.Media_menu_safinfo, curType));
        list.add(new MenuItem(MenuManager.MenuType.FILTERS, ResourceTable.Media_menu_general_anno, curType));
        list.add(new MenuItem(MenuManager.MenuType.SPTIAL_CONV, ResourceTable.Media_menu_general_anno, curType));
        list.add(new MenuItem(MenuManager.MenuType.BINARY, ResourceTable.Media_menu_general_anno, curType));
        list.add(new MenuItem(MenuManager.MenuType.HIST, ResourceTable.Media_menu_general_anno, curType));
        list.add(new MenuItem(MenuManager.MenuType.TEMPLATE_MATCH, ResourceTable.Media_menu_general_anno, curType));
        list.add(new MenuItem(MenuManager.MenuType.PIXEL_OPERATOR, ResourceTable.Media_menu_general_anno, curType));
        ListContainer container = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        container.setItemProvider(new DrawerProvider(list));
        container.setItemClickedListener((listContainer, component, index, len) -> {
            if (itemClickedListener != null) {
                itemClickedListener.onClick(list.get(index).menuType);
            }
        });
    }

    /**
     * setItemClickedListener
     *
     * @param itemClickedListener itemClickedListener
     */
    public void setItemClickedListener(ItemClickedListener itemClickedListener) {
        this.itemClickedListener = itemClickedListener;
    }

    /**
     * ItemClickedListener
     */
    public interface ItemClickedListener {
        void onClick(MenuManager.MenuType menuType);
    }
}